$(function() {
    //应参未参人员统计数据
    var statisticsData = [];
    //工伤保险人员数据
    var participleData = [];
    //用于存放查询的结果
    var searchParticipleList = [];
    //表格对象
    var participleTable = null;
    //导出文件的标题
    var title = "参保扩面资源";
    //是否用到查询  全部查询ifSearchAll=true,部分查询ifSearchAll=false;
    var ifSearchAll = true;
    statisticsData = [{
            id: '001',
            cityName: '雪峰镇',
            shouldParticiple: 10000,
            notParticiple: 1800
        },
        {
            id: '002',
            cityName: '盖山镇',
            shouldParticiple: 8000,
            notParticiple: 100
        },
        {
            id: '003',
            cityName: '仓山镇',
            shouldParticiple: 20020,
            notParticiple: 1499
        },
        {
            id: '004',
            cityName: '金山镇',
            shouldParticiple: 59032,
            notParticiple: 6400
        }
    ];
    participleData = [{
        id: '8020',
        name: '陈舒珠',
        idenifyCode: '3508234567844567',
        town: '车坡街',
        village: '西岸社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省内',
        address: '天河区车陂街金穗路70号之二2906房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8030',
        name: '贤心',
        idenifyCode: '3524234567844567',
        town: '车坡街',
        village: '美好社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省外',
        address: '天河区车陂街中山大道西55号华师大南区12栋402房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8040',
        name: '李四',
        idenifyCode: '3508234533844567',
        town: '车坡街',
        village: '美好社区',
        persionIden: '专业技术人员',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省内',
        address: '天河区车陂街金穗路70号之二2906房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8050',
        name: '吴荣连',
        idenifyCode: '350628199208148976',
        town: '车坡街',
        village: '美好社区',
        persionIden: '自由职业者',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省外',
        address: '天河区车陂街黄埔大道西601号暨大苏州苑13栋203房',
        noJoinReasion: '出国（含留学、定居、就业）'
    }, {
        id: '8060',
        name: '杨月芳',
        idenifyCode: '3508234567844123',
        town: '车坡街',
        village: '美好社区',
        persionIden: '自由职业者',
        employState: '无单位',
        survivalSate: '死亡',
        provinceMark: '省内',
        address: '天河区车陂街黄埔大道西601号暨大羊城苑13栋504房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8070',
        name: '王佳佳',
        idenifyCode: '3508234567666567',
        town: '车坡街',
        village: '美好社区',
        persionIden: '学生',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省内',
        address: '天河区车陂街枫叶路8号之四2505房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8080',
        name: '叶丽萍',
        idenifyCode: '350823456784434x',
        town: '车坡街',
        village: '美好社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省外',
        address: '天河区车陂街美景街62号604房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8090',
        name: '戴燕',
        idenifyCode: '3508114567844567',
        town: '车坡街',
        village: '美好社区',
        persionIden: '自由职业者',
        employState: '无单位',
        survivalSate: '死亡',
        provinceMark: '省内',
        address: '天河区车陂街花生寮二巷6号',
        noJoinReasion: '个人未参保'
    }, {
        id: '8110',
        name: '吴明',
        idenifyCode: '359506188309127845',
        town: '车坡街',
        village: '美好社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '死亡',
        provinceMark: '省内',
        address: '天河区车陂街龙口横街28号2栋503房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8130',
        name: '刘琴',
        idenifyCode: '350893165509119873',
        town: '车坡街',
        village: '美好社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省内',
        address: '天河区车陂街天河南一路18号',
        noJoinReasion: '个人未参保'
    }, {
        id: '8150',
        name: '赖业',
        idenifyCode: '350812187705054567',
        town: '车坡街',
        village: '美好社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省外',
        address: '天河区车陂街五山路381号华工2013级电信学院',
        noJoinReasion: '个人未参保'
    }, {
        id: '8170',
        name: '李椎',
        idenifyCode: '351223199006129876',
        town: '车坡街',
        village: '美好社区',
        persionIden: '现役军人',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省外',
        address: '天河区车陂街华景街39号之五101房',
        noJoinReasion: '个人未参保'
    }, {
        id: '8190',
        name: '王亮亮',
        idenifyCode: '319234188709090055',
        town: '车坡街',
        village: '美好社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '死亡',
        provinceMark: '省内',
        address: '天河区车陂街棠下达善大街8巷3号',
        noJoinReasion: '个人未参保'
    }, {
        id: '8210',
        name: '欧阳顺毅',
        idenifyCode: '358712199802018934',
        town: '车坡街',
        village: '美好社区',
        persionIden: '农民',
        employState: '无单位',
        survivalSate: '正常',
        provinceMark: '省外',
        address: '天河区车陂街华景街39号之五102房',
        noJoinReasion: '个人未参保'
    }];
    //layui初始化
    layui.use(['jquery', 'form', 'layer', 'table'], function() {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        //应参未参人员表格初始化
        table.render({
            elem: '#statisticsTable',
            page: false,
            cols: [
                [
                    { field: 'cityName', title: '城镇', width: '48%', fixed: 'left' },
                    { field: 'shouldParticiple', title: '应保人数', width: '25%', sort: true },
                    { field: 'notParticiple', title: '未保人数', width: '26%', style: 'color:red;font-size:18px;' }
                ]
            ],
            data: statisticsData
        });
        //自定义校验规则
        form.verify({
            idenify: [/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, '请输入有效的证件号'],
        });
        //监听提交
        form.on('submit(demo1)', function(formData) {
            var participleLoad = layer.load();
            //清空上一次查询的结果集
            searchParticipleList = [];
            var submitData = formData.field;
            console.log(submitData);
            if (submitData.cause || submitData.employment || submitData.idenify || submitData.individual || submitData.quiz1 || submitData.quiz2) {
                ifSearchAll = false;
            } else {
                ifSearchAll = true;
            }
            $.each(participleData, function(index, item) {
                if (submitData.idenify && submitData.idenify == item.idenifyCode && submitData.individual == "") {
                    searchParticipleList.push(item);
                } else if (submitData.individual && submitData.individual == item.persionIden && submitData.idenify == "") {
                    searchParticipleList.push(item);
                } else if (submitData.idenify && submitData.individual && submitData.idenify == item.idenifyCode && submitData.individual == item.persionIden) {
                    searchParticipleList.push(item);
                }
            });

            //重绘表格
            if (participleTable != null) {
                participleTable.clear();
                participleTable.destroy();
            }
            initParticiTable();
            //只为了演示加载效果
            setTimeout(function() {
                layer.close(participleLoad);
            }, 500);
            return false;
        });
    });
    //应保未保人员统计初始化
    function initStatistics() {
        for (var i = 0; i < statisticsData.length; i++) {
            var obj = statisticsData[i];
            var newNode = $("<div class='should-participle-body'><span class='title-city'>" +
                obj.cityName +
                "</span><span class='title-total'>" +
                obj.shouldParticiple +
                "</span><span class='title-no-par' style='color:red;font-size:16px;'>" +
                obj.notParticiple +
                "</span></div>");

            $("#statisticsDiv").append(newNode);
        }
    };
    //应保未保人员表格初始化
    function initParticiTable() {
        participleTable = $("#participleTable").DataTable({
            searching: false,
            ordering: false,
            lengthChange: false,
            //国际化
            language: {
                search: "在表格中搜索:",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
            },
            columns: [{
                "data": "id",
                "render": function(data, type, full, meta) {
                    return '<input type="checkbox"  class="checkchild" lay-skin="primary" value="' + data + '" />';
                }
            }, {
                data: 'name',
                /*title:'姓名',*/
                width: '8%'
            }, {
                data: 'idenifyCode',
                /*title:'身份证号码',*/
                width: '11%'
            }, {
                data: 'town',
                /*title:'镇',*/
                width: '10%'
            }, {
                data: 'village',
                /* title:'村（社区）',*/
                width: '13%'
            }, {
                data: 'employState',
                width: '8%',
                /*title:'就业状态'*/
            }, {
                data: 'survivalSate',
                width: '8%',

            }, {
                data: 'persionIden',
                /*title:'个人身份',*/
                width: '10%'
            }, {
                data: 'provinceMark',
                width: '8%'
            }, {
                data: 'address',
                width: '16%'
            }, {
                data: 'noJoinReasion',
                /*title:'未参保原因',*/
                width: '10%'
            }],
            data: ifSearchAll ? participleData : searchParticipleList,
            //导出
            dom: 'Bfrtip',
            buttons: [
                { extend: 'excel', text: '导出为EXCEL', title: title },
                { extend: 'csv', text: '导出为csv', title: title },
                { extend: 'print', text: '打印', title: title }
            ]
        });
        /*if(participleTable.init){
         layer.close(participleLoad);
         }*/
    };
    //复选框全选
    $(".checkall").click(function() {
        var check = $(this).prop("checked");
        //console.log(check)
        $(".checkchild").prop("checked", check);

    });

    //行内的选择框选中
    $(".checkchild").on("click", function() {
        var check = $(this).prop("checked");
        if (check && check == true) {
            $(this).prop("checked", false);
            $(".checkall").prop("checked", false);
        } else {
            $(this).prop("checked", true);
        }
    });
    //初始化
    function init() {
        initStatistics();
        initParticiTable();
    };
    init();
});